package cc.rengu.igas.opms.core.service.trans.wechat;

import cc.rengu.chargecalc.mchntfee.CalcMchntFee;
import cc.rengu.igas.channel.wechat.facade.response.WechatUnifiedOrderResponse;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.igas.opms.common.entity.TransLimitBean;
import cc.rengu.igas.opms.common.enums.RespCodeEnum;
import cc.rengu.igas.opms.core.service.base.PayService;
import cc.rengu.igas.opms.facade.request.WechatAppPayRequest;
import cc.rengu.igas.opms.facade.response.WechatAppPayResponse;
import cc.rengu.igas.share.common.constant.ShareParamConstant;
import cc.rengu.igas.share.common.constant.TransCacheConstant;
import cc.rengu.igas.share.core.model.MchntInfo;
import cc.rengu.igas.share.core.realize.MchntService;
import cc.rengu.igas.share.core.realize.impl.MchntServiceImpl;
import cc.rengu.igas.share.core.service.channel.momp.ComboPaymentService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.MchntOrderDetailMapper;
import cc.rengu.oltp.service.common.dao.impl.MchntOrderDetailMapperImpl;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.service.common.entity.MchntOrderDetail;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.service.common.enums.*;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.OrderInfo;
import cc.rengu.oltp.service.realize.OrderService;
import cc.rengu.oltp.service.realize.TransCustomImpl;
import cc.rengu.oltp.service.realize.impl.OrderServiceImpl;
import cc.rengu.oltp.utility.util.*;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static cc.rengu.igas.channel.upac.common.constant.UpacTreeNodeConstant.bizType;

/**
 * 微信APP支付
 * Created by  liujinan on 2020/4/23.
 */
public class WechatAppPayService extends PayService implements TransCustomImpl {
    /*购买卡卷交易标识*/
    boolean isBuyCouponTrans = false;

    @Override
    protected Object bizBeforeCust() throws Exception {
        WechatAppPayRequest wechatAppPayRequest = new WechatAppPayRequest();
        ConvertUtil.convertOutput(wechatAppPayRequest);
        if (!StringUtil.isEmptyOrNull(wechatAppPayRequest.getOpenId())) {
            byte[] bytes = Base64.decodeBase64(wechatAppPayRequest.getOpenId().getBytes("utf8"));
            wechatAppPayRequest.setOpenId(new String(bytes));
        }
        return wechatAppPayRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);

        WechatAppPayRequest wechatAppPayRequest = (WechatAppPayRequest) request;
        String instId = wechatAppPayRequest.getHeader().getInstId();
        String txnNum = wechatAppPayRequest.getHeader().getTxnNum();
        String bizType = wechatAppPayRequest.getHeader().getBizType();

        // 获取用户订单，检查订单可支付性
        OrderService orderService = new OrderServiceImpl();
        OrderInfo orderInfo = orderService.selectOrderInfoByUserOrderId(instId, wechatAppPayRequest.getUserOrderId());
        if (null == orderInfo) {
            rglog.error("订单信息不存在!");
            throw new BizException(RespCodeEnum.ORDER_NOEXIT.getRespCode(), RespCodeEnum.ORDER_NOEXIT.getRespDesc());
        }
        UserOrderInfo userOrderInfo = orderInfo.getUserOrderInfo();
        /*支付备注*/
        userOrderInfo.setOrderDesc(wechatAppPayRequest.getOrderDesc());
        // 根据用户订单表的系统流水号获取商户订单
        List<MchntOrderInfo> mchntOrderInfoList = orderInfo.getMchntOrderInfoList();

        //用户是否使用卡券
        boolean isCouponTrans = null != wechatAppPayRequest.getCouponInfo() && !StringUtil.isEmptyOrNull(wechatAppPayRequest.getCouponInfo().getDiscountId());

        //判断是否是购买卡券的交易
        String transMchntNo;
        if ("00".equals(mchntOrderInfoList.get(0).getBizSceneType()) || "02".equals(mchntOrderInfoList.get(0).getBizSceneType())) {
            isBuyCouponTrans = true;
        }
        transMchntNo = mchntOrderInfoList.get(0).getMchntNo();

        //购买卡券的交易不支持使用卡券
        if (isCouponTrans && isBuyCouponTrans) {
            rglog.info("购买卡券的交易不支持使用卡券");
            throw new BizException(RespCodeEnum.NOT_SURP_TRANS_TYPE_ERROR.getRespCode(), RespCodeEnum.NOT_SURP_SIGNTOEKN_TYPE_ERROR.getRespDesc());
        }

        //商户权限校验及商户订单转换 区分购买卡券交易和普通交易
        MchntInfo mchntInfo;
        List<TransLimitBean> transLimitBeanList = new ArrayList<>();
        if (isBuyCouponTrans) {
            mchntInfo = buyCouponTransConvertMchntOrder(mchntOrderInfoList, transLimitBeanList, wechatAppPayRequest, transMchntNo, userOrderInfo.getOrderAmt());
        } else {
            mchntInfo = unionMchntOrderConvert(mchntOrderInfoList, transLimitBeanList, wechatAppPayRequest);
        }
        /*更新订单明细表*/
        List<MchntOrderDetail> mchntOrderDetailList = new ArrayList<>();
        for (MchntOrderInfo mchntOrderInfo : mchntOrderInfoList) {

            mchntOrderInfo.setBizProdCode(txnInfo.getSrcTxnAuthCfg().getBizProdCode());
            mchntOrderInfo.setTxnNum(txnNum);
            mchntOrderInfo.setBizType(bizType);
            mchntOrderInfo.setSysSeqNum(sysSeqNum);
            mchntOrderInfo.setMchntDiscountFee("0.00");
            mchntOrderInfo.setTransStatus(TransStatusEnum.TRANS_INIT.getStatus());
            mchntOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
            mchntOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));

            MchntOrderDetailMapper mchntOrderDetailMapper = new MchntOrderDetailMapperImpl();
            List<MchntOrderDetail> oneMchntOrderDetailList = mchntOrderDetailMapper.selectMchntOrderDetailByMchntOrderId(mchntOrderInfo.getInstId(), mchntOrderInfo.getMchntNo(), mchntOrderInfo.getMchntOrderId());
            if (!CollectionUtils.isEmpty(oneMchntOrderDetailList)) {
                for (MchntOrderDetail mchntOrderDetail : oneMchntOrderDetailList) {
                    mchntOrderDetail.setSysSeqNum(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
                }
                mchntOrderDetailList.addAll(oneMchntOrderDetailList);
            }
        }
        if (!CollectionUtils.isEmpty(mchntOrderDetailList)) {
            orderService.updateOrderInfo(null, null, mchntOrderDetailList, null);
        }
        //更新用户订单表
        if (!updateOrderInfo(userOrderInfo, mchntOrderInfoList, null)) {
            rglog.error("更新订单信息失败");
            throw new BizException(RespCodeEnum.UPDATE_ORDER_STATUS_ERROR.getRespCode(), RespCodeEnum.UPDATE_ORDER_STATUS_ERROR.getRespDesc());
        }

        orderInfo.setMchntOrderInfoList(mchntOrderInfoList);
        orderInfo.setUserOrderInfo(userOrderInfo);
        ComboPaymentService comboPaymentService = new ComboPaymentService();
        comboPaymentService.callService(txnInfo, orderInfo, ShareParamConstant.CALL_CFG_DEFAULT_BIZ_SCENE, request);

        BizResponse<WechatAppPayResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        WechatAppPayResponse wechatAppPayResponse = (WechatAppPayResponse) GlobalTransUtil.getCacheValue(TransCacheConstant.BIZ_RESPONSE);
        if (null == wechatAppPayResponse) {
            wechatAppPayResponse = new WechatAppPayResponse();
        }
        wechatAppPayResponse.setHeader(wechatAppPayRequest.getHeader());
        //响应信息
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(wechatAppPayResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        WechatAppPayResponse wechatAppPayResponse = (WechatAppPayResponse) ((BizResponse) response).getResult();
        ConvertUtil.convertInput(wechatAppPayResponse);
    }

    /**
     * 购买卡券交易转换商户订单-权限检查/商户限额检查/获取微信商户
     *
     * @param mchntOrderInfoList 商户订单
     * @param transLimitBeanList 交易限额数据集合
     * @param request            请求体
     * @param transMchntNo       交易商户
     * @param orderAmt           订单金额
     * @return 微信商户号
     * @throws Exception 异常
     */
    private MchntInfo buyCouponTransConvertMchntOrder(List<MchntOrderInfo> mchntOrderInfoList, List<TransLimitBean> transLimitBeanList,
                                                      WechatAppPayRequest request, String transMchntNo, String orderAmt) throws Exception {

        MchntService mchntService = new MchntServiceImpl();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        /*卡券购买交易，不计算商户手续费，只需检查一次商户权限，商户号取自用户订单表*/
        MchntInfo mchntInfo = mchntService.getMchntInfo(request.getHeader().getInstId(), transMchntNo);
        if (null == mchntInfo) {
            rglog.error("查询商户基本信息失败<{}>", transMchntNo);
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        /*交易权限检查*/
        boolean checkResult = mchntService.checkMchntAuth(request.getHeader().getInstId(), transMchntNo, null, request.getHeader().getTxnNum(), request.getHeader().getBizType(), AcctTypeEnum.ALL.getAcctType());
        if (!checkResult) {
            rglog.error("商户<{}>无此交易权限!", transMchntNo);
            throw new BizException(RespCodeEnum.NOT_SURP_TRANS_TYPE_ERROR.getRespCode(), RespCodeEnum.NOT_SURP_TRANS_TYPE_ERROR.getRespDesc());
        }
        /*限额检查*/
        int limitFlag = mchntService.checkMchntTransLimit(request.getHeader().getInstId(), orderAmt, LimitAcctTypeEnum.ALL, transMchntNo, null, request.getHeader().getTxnNum());
        if (limitFlag == 2 || limitFlag == 3) {
            rglog.error("商户金额超限");
            throw new BizException(RespCodeEnum.MCHNT_AMT_OVER.getRespCode(), RespCodeEnum.MCHNT_AMT_OVER.getRespDesc());
        } else if (limitFlag == 1) {
            /*不支持信用卡*/
            xmlTreeUtil.setXmlTreeStringValue("limitPay", "no_credit");
        }

        for (MchntOrderInfo mchntOrderInfo : mchntOrderInfoList) {
            mchntOrderInfo.setBizProdCode(txnInfo.getSrcTxnAuthCfg().getBizProdCode());
            mchntOrderInfo.setTxnNum(request.getHeader().getTxnNum());
            mchntOrderInfo.setBizType(request.getHeader().getBizType());
            mchntOrderInfo.setSysSeqNum(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
            mchntOrderInfo.setTransStatus(TransStatusEnum.TRANS_INIT.getStatus());
            mchntOrderInfo.setMchntDiscountFee("0.00");
            mchntOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
            mchntOrderInfo.setCreateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        }

        //查询微信商户信息
        return mchntService.getMchntInfo(request.getHeader().getInstId(), transMchntNo);
    }

    /**
     * 非购买卡券的交易 校验商户交易权限/商户限额检查/计算商户手续费/获取微信商户号
     *
     * @param mchntOrderInfoList 商户订单集合
     * @param transLimitBeanList 商户限额集合
     * @param request            请求体
     * @return 微信商户信息
     * @throws Exception 异常
     */
    private MchntInfo unionMchntOrderConvert(List<MchntOrderInfo> mchntOrderInfoList, List<TransLimitBean> transLimitBeanList, WechatAppPayRequest request) throws Exception {

        MchntService mchntService = new MchntServiceImpl();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int limitFlag = 0;
        //平台商户列表
        List<String> platMchntNoList = new ArrayList<>();
        //微信子商户列表
        List<String> wechatMchntNoList = new ArrayList<>();

        //遍历商户订单，检查商户交易权限，商户限额检查，计算商户手续费
        CalcMchntFee clearMethod = new CalcMchntFee();
        String currentDateTime = DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS");

        for (MchntOrderInfo mchntOrderInfo : mchntOrderInfoList) {
            MchntInfo mchntInfo = mchntService.getMchntInfo(mchntOrderInfo.getInstId(), mchntOrderInfo.getMchntNo());
            if (null == mchntInfo) {
                rglog.error("查询商户基本信息失败<{}>", mchntOrderInfo.getMchntNo());
                throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
            }
            if (mchntOrderInfoList.size() > 1 && StringUtil.isEmptyOrNull(mchntInfo.getPlatMchntNo())) {
                rglog.info("微信APP支付必须以平台商户作为载体，当前商户:<{}>不存在平台商户，交易拒绝", mchntOrderInfo.getMchntNo());
                throw new BizException(RespCodeEnum.MCHNT_NO_PERMISSION.getRespCode(), RespCodeEnum.MCHNT_NO_PERMISSION.getRespDesc());
            }
            if (StringUtil.isEmptyOrNull(mchntInfo.getPlatMchntNo())) {
                //普通商户  获取微信商户信息
                mchntInfo = mchntService.getMchntInfo(request.getHeader().getInstId(), mchntInfo.getMchntNo());
            } else {
                platMchntNoList.add(mchntInfo.getPlatMchntNo());
                //获取平台商户微信商户信息
                mchntInfo = mchntService.getMchntInfo(request.getHeader().getInstId(), mchntInfo.getPlatMchntNo());
            }
            if ("11".equals(mchntInfo.getMchntType()) && !AppParamConstant.YES.equals(mchntInfo.getIndependentDataFlag())) {
                rglog.debug("商户为门店商户且不是独立维护资料，通过连锁商户号获取微信商户信息");
                mchntInfo = mchntService.getMchntInfo(request.getHeader().getInstId(), mchntInfo.getChainMchntNo());
            }

            //获取微信商户号 若不是独立维护资料的连锁门店商户则以连锁商户号去查询微信商户号
//            if ("11".equals(opmsMchntBaseInfo.getMchntType()) && !AppParamConstant.YES.equals(opmsMchntBaseInfo.getIndependentDataFlag())) {
//                rglog.debug("商户为门店商户且不是独立维护资料，通过连锁商户号获取微信商户信息");
//                wechatMchntInfo = wechatMchntInfoMapper.selectWechatMchntInfoByPrimaryKey(request.getHeader().getInstId(), opmsMchntBaseInfo.getChainMchntNo());
//            } else {
//                rglog.debug("商户有所属平台商户,通过平台商户获取微信商户信息");
//                wechatMchntInfo = wechatMchntInfoMapper.selectWechatMchntInfoByPrimaryKey(request.getHeader().getInstId(), opmsMchntBaseInfo.getMchntNo());
//            }
            if (null != mchntInfo) {
                wechatMchntNoList.add(mchntInfo.getWechatMchntNo());
            }

            //交易权限检查
            boolean checkResult = mchntService.checkMchntAuth(request.getHeader().getInstId(), mchntOrderInfo.getMchntNo(), null, request.getHeader().getTxnNum(), mchntOrderInfo.getBizType(), AcctTypeEnum.ALL.getAcctType());
            if (!checkResult) {
                rglog.error("商户<{}>无此交易权限!", mchntOrderInfo.getMchntNo());
                throw new BizException(RespCodeEnum.NOT_SURP_TRANS_TYPE_ERROR.getRespCode(), RespCodeEnum.NOT_SURP_TRANS_TYPE_ERROR.getRespDesc());
            }
            //限额检查
            /*TransLimitBean transLimitBean = mchntService.checkMchntTransLimit(request.getHeader().getInstId(), mchntOrderInfo.getOrderAmt(),
                    mchntOrderInfo.getMchntNo(), opmsMchntBaseInfo.getMchntLevel(), AcctTypeEnum.ALL.getAcctType(), request.getHeader().getTxnNum());
            transLimitBeanList.add(transLimitBean);*/
            /*商户限额处理,判断交易是否支持借记卡和贷记卡*/
            limitFlag = mchntService.checkMchntTransLimit(request.getHeader().getInstId(), mchntOrderInfo.getOrderAmt(), LimitAcctTypeEnum.ALL, mchntOrderInfo.getMchntNo(), null, request.getHeader().getTxnNum());
            if (limitFlag == 2 || limitFlag == 3) {
                rglog.error("商户金额超限");
                throw new BizException(RespCodeEnum.MCHNT_AMT_OVER.getRespCode(), RespCodeEnum.MCHNT_AMT_OVER.getRespDesc());
            } else if (limitFlag == 1) {
                /*不支持信用卡*/
                xmlTreeUtil.setXmlTreeStringValue("limitPay", "no_credit");
            }
            //微信支付前不计算商户手续费，成功后计算

            mchntOrderInfo.setTransStatus(TransStatusEnum.TRANS_INIT.getStatus());
            mchntOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
            mchntOrderInfo.setUpdateTime(currentDateTime);
            mchntOrderInfo.setSysSeqNum(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
            mchntOrderInfo.setTxnNum(request.getHeader().getTxnNum());
            mchntOrderInfo.setBizType(bizType);
            mchntOrderInfo.setBizProdCode(txnInfo.getSrcTxnAuthCfg().getBizProdCode());
            //实时清算判断
            if (request.getCouponInfo() != null && request.getCouponInfo().getDiscountId() != null) {
                mchntOrderInfo.setRealSettleFlag(AppParamConstant.NO);
            } else {
                String realSettleFlag = mchntService.getRealSettleFlag(mchntInfo.getMchntNo(), txnInfo.getSrcTxnAuthCfg().getBizProdCode(), mchntOrderInfo.getBizSceneType(), mchntOrderInfo.getRealSettleAmt());
                mchntOrderInfo.setRealSettleFlag(realSettleFlag);
                if (AppParamConstant.YES.equals(mchntOrderInfo.getRealSettleFlag())) {
                    mchntOrderInfo.setRealSettleType("99");
                }
            }

            //判断商户是否是独立清算的门店商户
            if ("11".equals(mchntInfo.getMchntType()) && AppParamConstant.NO.equals(mchntInfo.getIndependentDataFlag())) {
                rglog.debug("非独立维护资料的门店商户商户订单表记录其连锁商户号");
                mchntOrderInfo.setChainMchntNo(mchntInfo.getChainMchntNo());
            }
        }

        //合并订单支付必须同属于一个平台商户且不能夹杂子商户微信的
        if (mchntOrderInfoList.size() > 1) {
            platMchntNoList = platMchntNoList.stream().distinct().collect(Collectors.toList());
            if (platMchntNoList.size() != 1) {
                rglog.info("多商户合并订单支付必须同属一个平台商户:<{}>", platMchntNoList.get(0));
                throw new BizException(RespCodeEnum.MCHNT_NO_PERMISSION.getRespCode(), RespCodeEnum.MCHNT_NO_PERMISSION.getRespDesc());
            }
            //多商户合并支付时商户到微信进件的商户号必须一致
            if (!wechatMchntNoList.isEmpty()) {
                if (wechatMchntNoList.size() != mchntOrderInfoList.size()) {
                    rglog.info("多商户合并订单支付必须同属于一个微信子商户,不允许某个商户未到微信商户进件");
                    throw new BizException(RespCodeEnum.MCHNT_NO_PERMISSION.getRespCode(), RespCodeEnum.MCHNT_NO_PERMISSION.getRespDesc());
                } else {
                    wechatMchntNoList = wechatMchntNoList.stream().distinct().collect(Collectors.toList());
                    if (wechatMchntNoList.size() != 1) {
                        rglog.info("多商户合并订单支付必须同属于一个微信子商户");
                        throw new BizException(RespCodeEnum.MCHNT_NO_PERMISSION.getRespCode(), RespCodeEnum.MCHNT_NO_PERMISSION.getRespDesc());
                    }
                }
            }

            /*合并订单支付，不能合并特殊费率跟非特殊费率，不能合并d0清算跟非d0清算*/
            List<MchntOrderInfo> mchntOrderInfoSpFeeList = mchntOrderInfoList.stream().filter(item -> "0".equals(item.getFeeInputMode())).collect(Collectors.toList());
            if (!mchntOrderInfoSpFeeList.isEmpty() && mchntOrderInfoSpFeeList.size() != mchntOrderInfoList.size()) {
                rglog.error("合并订单中存在特殊费率跟非特殊费率的订单，拒绝交易");
                throw new BizException(RespCodeEnum.ORDER_NOT_ALLOW_PAY.getRespCode(), RespCodeEnum.ORDER_NOT_ALLOW_PAY.getRespDesc());
            }
            List<MchntOrderInfo> mchntOrderInfoD0List = mchntOrderInfoList.stream().filter(item -> AppParamConstant.YES.equals(item.getRealSettleFlag())).collect(Collectors.toList());
            if (!mchntOrderInfoD0List.isEmpty() && mchntOrderInfoD0List.size() != mchntOrderInfoList.size()) {
                rglog.error("合并订单中存在d0清算跟非d0清算的订单，拒绝交易");
                throw new BizException(RespCodeEnum.ORDER_NOT_ALLOW_PAY.getRespCode(), RespCodeEnum.ORDER_NOT_ALLOW_PAY.getRespDesc());
            }
            //获取微信商户信息
            if (platMchntNoList.isEmpty()) {
                rglog.info("多商户合并订单支付必须同属于同一个平台商户");
                throw new BizException(RespCodeEnum.MCHNT_NO_PERMISSION.getRespCode(), RespCodeEnum.MCHNT_NO_PERMISSION.getRespDesc());
            } else {
                mchntInfo = mchntService.getMchntInfo(request.getHeader().getInstId(), platMchntNoList.get(0));
            }
        }

        //判断商户是否支持微信交易
        if (mchntInfo == null || StringUtil.isEmptyOrNull(mchntInfo.getWechatMchntNo())) {
            rglog.error("商户未开通微信业务");
            throw new BizException(RespCodeEnum.MCHNT_NO_PERMISSION.getRespCode(), RespCodeEnum.MCHNT_NO_PERMISSION.getRespDesc());
        }

        //存在进件的子商户，下单以该子商户的微信进件商户进行下单
        if (!wechatMchntNoList.isEmpty()) {
            rglog.info("存在进件的子商户，下单以该子商户的微信进件商户:<{}>进行下单", wechatMchntNoList.get(0));
            mchntInfo.setWechatMchntNo(wechatMchntNoList.get(0));
        }

        return mchntInfo;
    }

    @Override
    public ChannelCallCfg channelCallSceneDiscern(Object request, List<ChannelCallCfg> channelCallCfgList) throws Exception {
        Optional<ChannelCallCfg> channelCallCfgOpt = channelCallCfgList.stream().filter(item ->
                item.getBizScene().equals(ShareParamConstant.CALL_CFG_DEFAULT_BIZ_SCENE)).findFirst();
        return channelCallCfgOpt.orElse(null);
    }

    @Override
    public CheckResultEnum channelCallPreCheck(Object request, ChannelCallCfg channelCallCfg, OrderInfo orderInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        if (ChannelEnum.MOMP.getChannelType().equals(channelCallCfg.getCallChannelId())) {
            if (isBuyCouponTrans) {
                return CheckResultEnum.NEXT;
            }
        } else if (ChannelEnum.WXZF.getChannelType().equals(channelCallCfg.getCallChannelId()) ||
                ChannelEnum.UPWX.getChannelType().equals(channelCallCfg.getCallChannelId()) ||
                ChannelEnum.NUWX.getChannelType().equals(channelCallCfg.getCallChannelId())) {
            xmlTreeUtil.setXmlTreeStringValue("tradeType", "APP");
            xmlTreeUtil.setXmlTreeStringValue("openId", "");
        }
        return CheckResultEnum.PASSED;
    }

    @Override
    public TransStatusEnum channelCallResultProc(Object request, ChannelCallCfg channelCallCfg, TransStatusEnum transStatusEnum, Object channelResponse, OrderInfo needUpdateOrderInfo) throws Exception {
        if (TransStatusEnum.TRANS_SUCC.equals(transStatusEnum) && null != channelResponse) {
            Result<WechatUnifiedOrderResponse> callResult = (Result<WechatUnifiedOrderResponse>) channelResponse;
            WechatUnifiedOrderResponse wechatResult = callResult.getResult();
            WechatAppPayResponse wechatAppPayResponse = new WechatAppPayResponse();
            wechatAppPayResponse.setQrCode(wechatResult.getRespCode());
            wechatAppPayResponse.setPrePayId(wechatResult.getPrepayId());
            wechatAppPayResponse.setPayInfo(wechatResult.getWechatpayInfo());
            GlobalTransUtil.setCacheValue(TransCacheConstant.BIZ_RESPONSE, wechatAppPayResponse);
        }
        return transStatusEnum;
    }
}
